CS 211 Lesson 18
Complex and Integer Data Types
Quote:
Always do everything you ask of those you command. George S. Patton
Lesson Objectives:
Understand MATLAB's complex number data type
Be able to work with complex numbers
Be able to plot complex data
Understand MATLAB's integer data types
Be able to create and use "mixed mode" expressions
Lesson:
I. MATLAB Concepts
A. Data Types and Data Structures - an introduction to the next 5 lessons
- Computers store and manipulate data. Since there are different types of data in the world (e.g., text, whole numbers, fractions, music, video, etc.), computer programming languages implement a variety of data types to correctly deal with these different types of data.
- A data type defines 3 major properties of a data value:
- The amount of memory (number of bits) used for each unique value of this data type.
- What the memory bits mean -- which determines the range of possible values the data type can represent.
- What valid operations are permitted on values of this data type.
- The basic data types in MATLAB are:
Type of Data Data Type Number of bits (bytes)
used for one valueRange of values Valid operations real numbers double 64 (8) ()2.2251 x 10-308 to
1.7977 x 10308arithmetic and vector
algebra operationsreal numbers single 32 (4) ()1.17549 x 10-38 to
3.40282 x 10+38arithmetic and vector
algebra operationslogical (true/false) values logical 1 (1) 0 or 1 logical operators whole numbers int8 8 (1) -128 to +127 arithmetic operations whole numbers int16 16 (2) -32,768 to + 32,767 arithmetic operations whole numbers int32 32 (4) -2,147,483,648 to +2,147,483,647 arithmetic operations positive whole numbers uint8 8 (1) 0 to 255 arithmetic operations positive whole numbers uint16 16 (2) 0 to 65,535 arithmetic operations positive whole numbers uint32 32 (4) 0 to 4,294,967,295 arithmetic operations text char 16 (2) Unicode character encodings arithmetic operations
and string functions
- A data structure is a method of organizing multiple values into a single structure that facilitates data manipulate. It is not uncommon that data organized in one way is difficult to process, but organized in another way it becomes easier to process. It is a programmer's job to select appropriate data structures for their data based on the problem being solved.
- The basic data structures in MATLAB are:
Data Structure MATLAB name Comments scalar 1-by-1 array single values row vector 1-by-n array list of values column vector n-by-1 array list of values matrix n-by-m array grid of values multidimensional array n-by-m-by-p array
n-by-m-by-p-by-s array
etc.more than 2 indexes required to select a unique value out of the array sparse array (any of the above) an efficient way to store large arrays that contain very few non-zero elements cell array (any of the above) a collection of values, each possibly of a different data type and data structure structure array a list of "named" values a collection of values, each possibly of a different data type and data structure, that are identified by a unique field "name."
B. Complex numbers
- Review of complex numbers
- Complex numbers come from the fact that a simple equation like r2 + 1 = 0 has no solution from the real numbers. If you solve for r, the "answer" is the square root of -1, which does not exist. So mathematicians created a value for the square root of -1 and called it "i". But the square root of -1 does not exist! (You can't have everything.) Using i allows us to calculate answers to many equations that would not have any solutions otherwise. (Isn't it great when you can just make up things to get answers!).
- Complex numbers are critical to the analysis of many types of engineering systems, especially those that include phase shifts like signal analysis.
- (a + bi) is a complex number, where a and b are real values and i is the square root of -1.
- If plotted in rectangular coordinates, the value a is associated with the x-axis and the value b is associated with the y-axis.
- The magnitude of a complex number (a + bi) is sqrt(a2 + b2).
- The magnitude of a complex number is equal to the distance from the origin of the (x,y) point (a,b).
- The angle of (a + bi) is tan-1(b/a).
- Standard operations on complex numbers: assume that c1 = (a1 + b1i) and c2 = (a2 + b2i)
- c1 + c2 = (a1 + a2) + (b1 + b2)i
- c1 - c2 = (a1 - a2) + (b1 - b2)i
- c1 * c2 = (a1a2 - b1b2) + (a1b2 + b1a2)i
- c1 / c2 = (a1a2 - b1b2)/(a22 + b22) + (b1a2 - a1b2)/(a22 + b22)i
for example, execute these statements in MATLAB and examine the results
c = 2 + 3i
d = 3 + 4i
disp(c + d)
disp(c - d)
disp(c * d)
disp(c / d)
- MATLAB uses the symbols i and j to represent sqrt(-1).
- MATLAB functions (a subset) that return information about complex numbers
- real(c) returns the real part of a complex number c
- imag(c) returns the imaginary part of a complex number c
- isreal(c) returns true (1) if c is not complex
- abs(c) returns the magnitude of a complex number
- angle(c) returns the angle of a complex number
- Plotting complex numbers
- given the equations
t = 0:pi/20:4*pi;
y = exp(-0.2*t) .* (cos(t) + i*sin(t)); % any function that creates complex values
plot the real part only plot(t, y) plot the imaginary part only plot(t, imag(y)) plot real vs. imaginary plot(y) plot the complex values in polar coordinates polar(angle(y), abs(y)) C. Integer data types
- Integer values represent whole numbers with no fractional part.
- In MATLAB, integer values are used primary to represent image and sound data.
- Integer values use much less storage (memory) than double precision floating point values. If an application needs to manipulate large data sets, storing the data in an integer format can reduce memory usage.
- The types of integer values are shown below (Note that 'u' stands for 'unsigned', which means that all values are positive.)
Data type | Range of possible values | Memory usage per value |
int8 | -128 to +127 | 1 byte |
uint8 | 0 to 255 | 1 byte |
int16 | -32,768 to +32,767 | 2 bytes |
uint16 | 0 to 65,535 | 2 bytes |
int32 | -2,147,483,648 to +2,147,483,647 | 4 bytes |
uint32 | 0 to 4,294,967,295 | 4 bytes |
- By default all values in MATLAB are stored using the double data type. To change a value to a different data type, use a cast operation. All casts operators have the same name as their associated data type. For example:
a = int8(5) % a is now an int8 variable
whos ab = 2.8;
a = int16( (5.2 + b) / 2.5 ) % does double precision arithmetic and then converts the results to a 16 bit integer
whos a
- If all of the variables in an expression are of the same data type, then the results of the expression will be a value of that same data type. Execute the following two examples and examine the results:
a = int8(5)
b = int8(6)
c = a + b
whos a b ca = uint16(3280)
b = uint16(4376)
c = a + b
whos a b c
- A mixed mode expression is any expression that contains variables and values of different data types.
- Only one integer data type can be used in a single expression.
- A mixed mode expression can contain only one integer data type and doubles.
- The result of a mixed mode expression will always result in an integer value of the same integer data type used in the expression.
- Any double precision values in a mixed mode expression are rounded to their closest integer value before any operations are performed.
- Division using integer values rounds the results to the closest integer.
- Execute the following examples and examine the results:
a = int8(5) + int16(5); % generates a error because it contains two different integer data types
a = int8(5)
a = int8(10) / int8(15) % results in 1 because the division result is rounded to the closest integer
b = 6.7
c = a + b % results in 12 because the the 6.7 is rounded to 7 before the addition
- If integer values are manipulated, and the results become too large (or too small) for the data type being used, the results are clamped to the largest (or smallest) possible value for that data type. For example:
a = int8(100);
b = int8(100);
disp(a + b)
c = uint16(10000);
d = uint16(10);
disp(c * d)
- Most MATLAB built-in functions work on integer data types, but they typically return a value that is not an integer. Some MATLAB functions will not process integer data at all. For example:
a = sum( int8( [1 2 3 4 5 6] ) ) % returns an answer of type "double"
a = mean( int8( [1 2 3 4 5 6] ) ) % returns an answer of type "double"
a = sqrt( int8( [1 2 3 4 5 6] ) ) % generates an error message - invalid operation for integer data
II. Good Programming Practices
If you use I and J as variable names, make sure they are
capitalized so that they do not "hide" the symbols i and j
that are used for complex numbers.
Never write a mixed mode expression in MATLAB. Use casts such that every value in an expression has the same data type. For example:
Assume that b contains a int8 value,
c contains a double value, and d contains an
int8 value:
a = (b + c) / d % a
mixed mode expression - bad - produces an int8 result
a = (b + int8(c)) / d % all int8 values
- not mixed mode - produces an int8 result
a = (double(b) + c) / double(d) % all
double values - not mixed mode - produces a double result
III. Algorithms
(None for this lesson)
Lab Work: Lab 18
References: Chapman Textbook 6.1, 6.4 (3rd edition only) and http://en.wikipedia.org/wiki/Complex_number